import Vue from 'vue'
import router from './router'
import store from './store'
// import { mapActions } from 'vuex'
// import { timeFix } from '@/utils/util'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import notification from 'ant-design-vue/es/notification'
// import { ACCESS_TOKEN, WX_USER_ID, STAFF_INFO, WX_ACCESS_TOKEN } from '@/store/mutation-types'
import { ACCESS_TOKEN, WECHAT_INFO } from '@/store/mutation-types'
// import { wechatCheck } from '@/api/app'
import { login } from '@/api/login'

NProgress.configure({ showSpinner: false }) // NProgress Configuration

const whiteList = ['Login'] // no redirect whitelist

// 全局守卫 登录后通过这里第一次路由跳转 初始化用户数据全在这里
router.beforeEach((to, from, next) => {
  NProgress.start() // start progress bar
  if (Vue.ls.get(ACCESS_TOKEN)) {
    /* has token */
    if (to.path === '/user/login') {
      next({ path: '/workplace' })
      NProgress.done()
    } else {
      if (store.getters.roles.length === 0) {
        store
          .dispatch('GetInfo')
          .then(res => {
            store.dispatch('GenerateRoutes').then(() => {
              // 根据roles权限生成可访问的路由表
              // 动态添加可访问路由表
              router.addRoutes(store.getters.addRouters)
              const redirect = decodeURIComponent(from.query.redirect || to.path)
              // 重新登录时，不跳转到上一次访问的地址
              // const redirect = decodeURIComponent(to.path)
              if (to.path === redirect) {
                // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
                next({ ...to, replace: true })
              } else {
                // 跳转到目的路由
                next({ path: redirect })
              }
            })
          })
          .then(() => {
            // 进行websocket连接
            // Vue.prototype.SocketGlobal.connect()
          })
          .catch((e) => {
            // alert('catch' + e)
            // alert(JSON.stringify(e))
            /* notification.error({
              message: '错误',
              description: '请求用户信息失败，请重试3'
            }) */
            store.dispatch('Logout').then(() => {
              // next({ path: '/user/login', query: { redirect: to.fullPath } })
              if ((to.name && to.name.indexOf('app') === 0 && to.query.code)) {
                login({ userid: '', password: '' }, to.query.code, to.query.state).then(response => {
                  const result = response.result
                  Vue.ls.set(ACCESS_TOKEN, result.token, 7 * 24 * 60 * 60 * 1000)
                  store.commit('SET_TOKEN', result.token)
                  const staffInfo = {}
                  staffInfo.name = result.name
                  staffInfo.avatar = result.avatar
                  staffInfo.position = result.position
                  staffInfo.roles = result.roles
                  Vue.ls.set(WECHAT_INFO, staffInfo, 7 * 24 * 60 * 60 * 1000)
                  store.commit('SET_INFO', staffInfo)
                  next({ path: to.fullPath })
                  // eslint-disable-next-line handle-callback-err
                }).catch(error => {
                })
              } else {
                notification.error({
                  message: '错误',
                  description: '请求用户信息失败，请重试'
                })
                next({ path: '/user/login' })
              }
            })
          })
      } else {
        next()
      }
    }
  } else {
    if (whiteList.includes(to.name) || (to.name && to.name.indexOf('app') === 0)) {
      // zjt add
      if (to.name !== 'Login') {
        if (to.query.code === undefined || to.query.code === null || to.query.code === '') {
          next()
        } else {
          login({ userid: '', password: '' }, to.query.code, to.query.state).then(response => {
            const result = response.result
            Vue.ls.set(ACCESS_TOKEN, result.token, 7 * 24 * 60 * 60 * 1000)
            store.commit('SET_TOKEN', result.token)
            const staffInfo = {}
            staffInfo.name = result.name
            staffInfo.avatar = result.avatar
            staffInfo.position = result.position
            staffInfo.roles = result.roles
            Vue.ls.set(WECHAT_INFO, staffInfo, 7 * 24 * 60 * 60 * 1000)
            store.commit('SET_INFO', staffInfo)
            next({ path: to.fullPath })
            // eslint-disable-next-line handle-callback-err
          }).catch(error => {
          })
          /* wechatCheck(to.query).then((res) => {
            if (res.msg === 'success') {
              store.commit('SET_INFO', res.staffInfo)
              store.commit('SET_WXID', res.wxUserId)
              store.commit('SET_TOKEN', res.wxAccessToken)
              next()
            } else {
              notification.error({
                message: '错误',
                description: '请求用户信息失败，请重试1'
              })
              next({ path: '/user/login' })
            }
          }).catch(() => {
            notification.error({
              message: '错误',
              description: '请求用户信息失败，请重试2'
            })
            next({ path: '/user/login' })
          }) */
        }
      } else {
        // 在免登录白名单，直接进入
        next()
      }
    } else {
      // next({ path: '/user/login', query: { redirect: to.fullPath } })
      next({ path: '/user/login' })
      NProgress.done() // if current page is login will not trigger afterEach hook, so manually handle it
    }
  }
})

router.afterEach(() => {
  NProgress.done() // finish progress bar
})

/**
 * Action 权限指令
 * 指令用法：
 *  - 在需要控制 action 级别权限的组件上使用 v-action:[method] , 如下：
 *    <i-button v-action:add >添加用户</a-button>
 *    <a-button v-action:delete>删除用户</a-button>
 *    <a v-action:edit @click="edit(record)">修改</a>
 *
 *  - 当前用户没有权限时，组件上使用了该指令则会被隐藏
 */
const action = Vue.directive('action', {
  bind: function (el, binding, vnode) {
    const name = vnode.context.$route.name
    let actions = []
    const getElemets = (data) => {
      return data.some(item => {
        if (item.name !== name) {
          if (item.children) {
            return getElemets(item.children)
          } else {
            return false
          }
        }
        actions = item.elements
        return true
      })
    }
    const actionName = binding.arg
    let menus = []
    store.getters.roles.forEach(item => {
      menus = menus.concat(item.menus)
    })
    getElemets(menus)
    const per = actions.some(item => {
      if (item.code === actionName && item.defaultCheck === true) {
        return true
      }
    })
    if (!per) {
      el.parentNode && el.parentNode.removeChild(el) || (el.style.display = 'none')
    }
  }
})

export {
  action
}
